import { confirm, gathering, gatheringPage } from '../services/finance';
import React from 'react';
import {Input, InputNumber, message, Radio, Select} from 'antd';
const { TextArea } = Input;
const { Option } = Select;
// 表格列信息
const columns = [
  {
    title: '发票号',
    dataIndex: 'invoiceNo',
    width: '120px',
    ellipsis: true,
  },
  {
    title: '开票资料',
    dataIndex: 'invoiceInfo',
    width: '120px',
    ellipsis: true,
  },
  {
    title: '发票金额',
    dataIndex: 'invoiceAmount',
    width: '120px',
    ellipsis: true,
  },
  {
    title: '订单金额',
    dataIndex: 'orderAmount',
    width: '120px',
    ellipsis: true,
  },
  {
    title: '已收金额',
    dataIndex: 'receiptAmount',
    width: '120px',
    ellipsis: true,
  },
  {
    title: '是否锁定',
    dataIndex: 'isLock',
    width: '120px',
    ellipsis: true,
  },
  {
    title: '回款单号',
    dataIndex: 'receiptNo',
    width: '120px',
    ellipsis: true,
  },
];

// 搜索框字段
const searchFields = [
  {
    cols: [
      {
        md: 6,
        label: '发票号',
        name: 'invoiceNo',
        component: <Input placeholder="请输入发票号" />,
      },
      {
        md: 6,
        label: '是否锁定',
        name: 'isLock',
        component: <Select>
          <Option value="1">锁定</Option>
          <Option value="0">未锁定</Option>
        </Select>
      },
    ],
  },
];

// 开票表单
const invoiceRows = [
  {
    cols: [
      {
        label: '发票金额',
        name: 'invoiceAmount',
        md: 12,
        rules: [{ required: true, message: '请输入发票金额！' }],
        component: <InputNumber readOnly={true} placeholder="请输入发票金额" />,
      },
      {
        label: '收款金额',
        name: 'receiptAmount',
        md: 12,
        rules: [{ required: true, message: '请输入收款金额！' }],
        component: <InputNumber placeholder="请输入收款金额" />,
      },
    ],
  },
  {
    cols: [
      {
        label: '发票号',
        name: 'invoiceNo',
        md: 12,
        rules: [{ required: true, message: '请输入发票号！' }],
        component: <Input readOnly={true} placeholder="请输入发票号" />,
      },
      {
        label: '是否收全',
        name: 'isPayFull',
        md: 12,
        rules: [{ required: true, message: '必选' }],
        component: (
          <Radio.Group defaultValue="FINISH">
            <Radio.Button value="FINISH">足额</Radio.Button>
            <Radio.Button value="INVOICE">非足额</Radio.Button>
          </Radio.Group>
        ),
      },
    ],
  },
  {
    cols: [
      {
        label: '收款单号',
        name: 'receiptNo',
        md: 12,
        rules: [{ required: true, message: '请输入开票资料！' }],
        component: <TextArea rows={4} />,
      },
      {
        label: '开票资料',
        name: 'invoiceInfo',
        md: 12,
        rules: [{ required: true, message: '请输入开票资料！' }],
        component: <TextArea rows={4} readOnly={true} />,
      },
    ],
  },
];
export default {
  namespace: 'finance',

  state: {
    searchFields: searchFields, // 搜索框信息
    searchValues: {}, // 搜索框里的值
    pageList: [], // 表格数据
    pagination: {
      pageSize: 10,
      current: 1,
      size: 'small',
      showSizeChanger: true,
      showQuickJumper: true,
    }, // 表格分布数据
    columns: columns, // 表格列信息
    selectionIds: [], // 表格选择ID
    selectedRows: [], // 表格选择行
    invoiceModal: false, // 开票弹窗
    invoiceForm: {}, // 申请费用表单数据
    invoiceRows: invoiceRows, // 是请费用表单行
  },
  effects: {
    // 获取分页数据
    *page({ payload }, { call, put }) {
      const res = yield call(gatheringPage, payload);
      yield put({
        type: 'page_r',
        payload: res,
      });
    },
    *gathering({ payload }, { call, put }) {
      const res = yield call(gathering, payload);
      yield put({
        type: 'audit_r',
        payload: res,
      });
    },
    *confirm({ payload }, { call, put }) {
      const res = yield call(confirm, payload);
      yield put({
        type: 'audit_r',
        payload: res,
      });
    },
  },
  reducers: {
    // 绑定搜索框数据
    handleSearchChange(state, action) {
      const { searchValues } = state;
      const newSearchForm = {
        ...searchValues,
        ...action.payload,
      };
      return {
        ...state,
        searchValues: newSearchForm,
      };
    },

    // 改变分页插件
    handlePagination(state, action) {
      const { payload } = action;
      const { pagination } = state;
      let newPagination = {
        ...pagination,
        ...payload,
      };
      return {
        ...state,
        pagination: newPagination,
      };
    },
    // 表格选中Id
    handleSelection(state, action) {
      const { selectedRowKeys, selectedRows } = action.payload;
      return {
        ...state,
        selectionIds: selectedRowKeys,
        selectedRows: selectedRows,
      };
    },

    handleInvoiceChange(state, action) {
      const { invoiceForm } = state;
      const { fields } = action.payload;
      const newModalForm = {
        ...invoiceForm,
        ...fields,
      };
      return {
        ...state,
        invoiceForm: newModalForm,
      };
    },

    initInvice(state, action) {
      let invoiceForm = {};
      const { fields } = action.payload;
      invoiceForm.invoiceInfo = {
        name: 'invoiceInfo',
        value: fields.invoiceInfo,
      };
      invoiceForm.isPayFull = {
        name: 'isPayFull',
        value: 'FINISH',
      };
      invoiceForm.invoiceNo = {
        name: 'invoiceNo',
        value: fields.invoiceNo,
      };
      invoiceForm.invoiceAmount = {
        name: 'invoiceAmount',
        value: fields.invoiceAmount,
      };
      return {
        ...state,
        invoiceForm,
      };
    },

    handleInvoiceModal(state, action) {
      const { payload } = action;
      return {
        ...state,
        invoiceModal: payload,
      };
    },
    // 分页数据请求回来后处理
    page_r(state, action) {
      const { payload } = action;
      if (payload && payload.code === 0) {
        const list = payload.result.records;
        const pagination = {
          pages: payload.result.pages,
          pageSize: payload.result.size,
          total: payload.result.total,
          current: payload.result.current,
          size: 'small',
          showSizeChanger: true,
          showQuickJumper: true,
        };
        return {
          ...state,
          pagination: pagination,
          pageList: list,
          editId: '',
          selectionIds: [],
          selectedRows: [],
          isDirty: false,
        };
      } else {
        return {
          ...state,
        };
      }
    },
    // 处理发送
    audit_r(state, action) {
      const { payload } = action;
      if (payload && payload.code === 0) {
        message.success('操作成功！');
      } else {
        message.error(`${payload.errorMsg}` ? `${payload.errorMsg}` : '操作失败！');
      }
      return {
        ...state,
        selectionIds: [],
        selectedRows: [],
      };
    },
  },
};
